#include "maintenance.h"
#include "ui_maintenance.h"
#include <QMessageBox>
#include "loginset.h"


maintenance::maintenance(QString name, QString pass, QString SERVER, QString PORT,QWidget *parent )
    : QMainWindow(parent)
    , ui(new Ui::maintenance)
{
    ui->setupUi(this);
    this->name = name;
    this->pass = pass;
    mysql.SERVER = SERVER;
    mysql.PORT = PORT;

    if(!mysql.connectMysql())
    {
        qDebug() << "数据库连接失败" << mysql.db.lastError().text();
        QMessageBox::critical(this,"连接失败","数据库连接失败，请检查IP和端口号是否正确");
    }

    kdcModel = new QSqlTableModel(this, mysql.db);
    // 设置列宽拉伸填充表格
    ui->kdcTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    // 设置为不可编辑
    ui->kdcTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    // 参数化查询
    SQL = "SELECT maintain_num, maintain_contact FROM maintain_info "
          "WHERE maintain_name = :name";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":name", this->name);

    // 执行查询
    if (!query.exec()) {
        qDebug() << "查询失败：" << query.lastError().text();
        QMessageBox::critical(nullptr, "错误", "数据加载失败: " + kdcModel->lastError().text());
    }
    query.next();
    // 获取数据
    contact = query.value("maintain_contact").toString();
    num = query.value("maintain_num").toString();


    // 更新UI
    ui->numTxt->setText(this->num);
    ui->contactTxt->setText(this->contact);
    ui->nameTxt->setText(this->name);
}

maintenance::~maintenance()
{
    delete ui;
}


void maintenance::on_pushButton_clicked()
{
    loginSet *L = new loginSet(this->mysql.SERVER,this->mysql.PORT,this); // 指定父对象
    L->show();
}


void maintenance::on_maintain_clicked()
{
    // 创建并配置模型
    kdcModel->setTable("kdc_info"); // 绑定到你的表名
    kdcModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    // 设置字段显示名称（与实际字段名无关，仅UI显示）
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_color"), Qt::Horizontal, "车辆颜色");
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_type"), Qt::Horizontal, "车辆类型");
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_booking"), Qt::Horizontal, "使用状况");
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_state"), Qt::Horizontal, "车辆状态");
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_id"), Qt::Horizontal, "车辆编号");
    kdcModel->setHeaderData(kdcModel->fieldIndex("kdc_exclusive"), Qt::Horizontal, "是否专属");

    // 设置筛选条件
    // 合并条件，注意字符串值用单引号包裹
    kdcModel->setFilter("kdc_booking != '空闲'");

    error = kdcModel->lastError();

    if(!kdcModel->select()) { // 必须显式加载数据[2](@ref)
        QMessageBox::critical(nullptr, "错误", "数据加载失败: " + kdcModel->lastError().text());
        return;
    }

    // 将模型设置给kdcTableView
    ui->kdcTableView->setModel(kdcModel);
}


void maintenance::on_stickers_clicked()
{
    // 创建并配置模型
    kdcModel->setTable("stickers"); // 绑定到你的表名
    kdcModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    // 设置字段显示名称（与实际字段名无关，仅UI显示）
    kdcModel->setHeaderData(kdcModel->fieldIndex("vip_name"), Qt::Horizontal, "会员名称");
    kdcModel->setHeaderData(kdcModel->fieldIndex("vip_data"), Qt::Horizontal, "车贴内容");
    kdcModel->setHeaderData(kdcModel->fieldIndex("vip_state"), Qt::Horizontal, "完成状况");

    // 设置筛选条件
    // 合并条件，注意字符串值用单引号包裹
    kdcModel->setFilter("vip_state = '未完成'");

    error = kdcModel->lastError();

    if(!kdcModel->select()) { // 必须显式加载数据[2](@ref)
        QMessageBox::critical(nullptr, "错误", "数据加载失败: " + kdcModel->lastError().text());
        return;
    }

    // 将模型设置给kdcTableView
    ui->kdcTableView->setModel(kdcModel);
}


void maintenance::on_maintainYes_clicked()
{
    QString tmp;
    tmp = ui->maintainTxt->text();
    // 参数化
    SQL = "UPDATE kdc_info SET "
          "kdc_state = '完好' "
          "WHERE kdc_id = :tmp AND kdc_state != '完好'";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":tmp", tmp);
    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","更新失败");
    }
    else
    {
        // 参数化
        SQL = "UPDATE maintain_info SET "
              "maintain_num = maintain_num + 1 "
              "WHERE maintain_name = :name ";
        QSqlQuery query;
        query.prepare(SQL);
        query.bindValue(":name", name);
        query.exec();
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
    }
}


void maintenance::on_maintainNo_clicked()
{
    QString tmp;
    tmp = ui->maintainTxt->text();
    // 参数化
    SQL = "UPDATE kdc_info SET "
          "kdc_state = '报废' "
          "WHERE kdc_id = :tmp AND kdc_state != '完好'";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":tmp", tmp);
    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","更新失败");
    }
    else
    {
        // 参数化
        SQL = "UPDATE maintain_info SET "
              "maintain_num = maintain_num + 1 "
              "WHERE maintain_name = :name ";
        QSqlQuery query;
        query.prepare(SQL);
        query.bindValue(":name", name);
        query.exec();
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
    }
}


void maintenance::on_stickersYes_clicked()
{
    QString tmp;
    tmp = ui->stickersTxt->text();
    // 参数化
    SQL = "UPDATE stickers SET "
          "vip_state = '已完成' "
          "WHERE vip_name = :tmp";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":tmp", tmp);
    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","更新失败");
    }
    else
    {
        // 参数化
        SQL = "UPDATE maintain_info SET "
              "maintain_num = maintain_num + 1 "
              "WHERE maintain_name = :name ";
        QSqlQuery query;
        query.prepare(SQL);
        query.bindValue(":name", name);
        query.exec();
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
    }
}


void maintenance::on_tabWidget_currentChanged(int index)
{
    // 参数化查询
    SQL = "SELECT maintain_num, maintain_contact FROM maintain_info "
          "WHERE maintain_name = :name";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":name", this->name);

    // 执行查询
    if (!query.exec()) {
        qDebug() << "查询失败：" << query.lastError().text();
        QMessageBox::critical(nullptr, "错误", "数据加载失败: " + kdcModel->lastError().text());
    }
    query.next();
    // 获取数据
    num = query.value("maintain_num").toString();

    // 更新UI
    ui->numTxt->setText(this->num);
}


void maintenance::on_nameTxtYes_clicked()
{
    QString old = name;
    name = ui->nameTxt->text();

    // 参数化查询
    SQL = "UPDATE user SET "
          "uname = :name "
          "WHERE uname = :old";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":name", name);
    query.bindValue(":old", old);

    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","修改失败,用户名已存在");
        ui->nameTxt->setText(old);
        name =  old;
    }
    else
    {
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
        ui->nameTxt->setText(name);
    }
}


void maintenance::on_contactTxtYes_clicked()
{
    QString old = contact;
    contact = ui->contactTxt->text();
    // 参数化查询
    SQL = "UPDATE maintain_info SET "
          "maintain_contact = :contact "
          "WHERE maintain_name = :name";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":name", name);
    query.bindValue(":contact", contact);
    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","更新成功");
        ui->contactTxt->setText(old);
        contact = old;
    }
    else
    {
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
        ui->contactTxt->setText(contact);
    }
}


void maintenance::on_passTxtYes_clicked()
{
    QString old = pass;
    pass = ui->passTxt->text();
    // 参数化
    SQL = "UPDATE user SET "
          "upwd = :pass "
          "WHERE uname = :name";
    QSqlQuery query;
    query.prepare(SQL);
    query.bindValue(":pass", pass);
    query.bindValue(":name", name);
    if(!query.exec())
    {
        qDebug() << "更新失败：" << query.lastError().text();
        QMessageBox::warning(this,"修改失败","更新失败");
        ui->passTxt->setText(old);
        pass = old;
    }
    else
    {
        qDebug() << "更新成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"更新成功","更新成功");
        ui->passTxt->setText(pass);
    }
}

